define([
        'app/utils/WebUtils',
        'app/service/GeneralServices',
        'underscore',
        'app/utils/UploadPPTUtils',
        'app/utils/WebSocketUtils',
        'app/utils/AMUICookieUtils',
        'app/utils/HttpClientUtils',
        'app/utils/GUID',
        'app/utils/CommonUtils'
        
        
        ], function (
        WebUtils,
        gs,
        _,
        UploadPPTUtils,
        WebSocketUtils,
        AMUICookieUtils,
        hc
    ) {
	
	var win = window;
	
	$("#qrcode").modal();
	$("#shortcode").keyup(function(){
		$(this).val($(this).val().toUpperCase());
		
		var code = $(this).val();
		if(code.length == 3){
			
			var ppt_auth_url = "luyan/ppt/verify/code/{0}";
			ppt_auth_url = ppt_auth_url.replace("{0}",code);
			hc.sendPOST(ppt_auth_url,{},function(resp){
				
				if(resp.status == "200"){
					win.userid = resp.result.userid;
					win.headimgurl = resp.result.headimgurl;
					win.name = resp.result.name;
					win.pptserver = resp.result.pptserver;
					win.callbackurl = resp.result.callbackurl;
					
					
					if(win.userid == null){
						alert("授权码不正确或超过60秒");
						$("#shortcode").val("");
					}else{

						$("#shortcode").prop("readonly",true);
						$("#success").show();
						
						
						var subscribe_pattern = "/ws/private/ppt/key/"+win.userid;
						//注册监听pattern
						WebSocketUtils.subscribe(subscribe_pattern, function(data_pack){
							
							var data = JSON.parse(data_pack.body);
							console.log(data);
							
							if(data.type == "auth_ppt"){
								
							}else if(data.type == "uploaded"){
								alert("上传成功");
								location.reload();
							}
							
							
						});
						
							
						var endpointurl = "appchat_websocket";
						 
						WebSocketUtils.connect(endpointurl, {},function(){
									
						},function(){
						//如果断开连接，则进行重连
						
						});
						
						
						
						setTimeout(function(){
							
							$("#headimgurl").attr("src",win.headimgurl);
							$("#username").text(win.name);
							$("#userinfo").show();
							$("#qrcode").modal();
							
							var reqbody = {};
							reqbody.type = "auth_ppt";
							WebSocketUtils.send("/appchat/private/ppt/key/"+win.userid, reqbody);
						},2000);
					}

				}else{
					
					alert("授权码不正确或超过60秒");
					$("#shortcode").val("");
				}
				
	
				
			},null,true);
			

		}
	});
	
	setTimeout(function(){
		$("#shortcode").focus();
		
	},500);
	
	
	
	var url = "/basic/native/upload_ppt/folder/ppt";
	UploadPPTUtils.init("ppt_upload_container","upload_btn",url,
		function(up, file, info){
			var resp = JSON.parse(info.response);
			var filename = file.name;
			console.log(resp);
			if(resp.status != "201"){
				
				
				setTimeout(function(){
					WebUtils.alert("上传失败，请重试");	
					$("#uploading").modal();
				
				},2000);
	
			}

			filename = filename.replace(".pptx","");
			filename = filename.replace(".ppt","");

			var ftype = 'ppt';
			var temp_filename = filename.replace(".pdf",""); 
			if(temp_filename != filename){
				filename = temp_filename;
				ftype = 'pdf';
			}
			var key = resp.result.key
			var ppturl = win.pptserver + "ppt_to_jpg";
			var callbackurl = win.callbackurl.replace("{0}",win.userid);
			callbackurl = callbackurl.replace("{1}",key);

			var pptbody = {
				"key": key,
				"callbackurl": callbackurl,
				"pptname": encodeURI(filename),
				"type": ftype
			};
			console.log(pptbody);
			hc.sendPOST(ppturl,pptbody,function(resp){
				console.log(resp);

				setTimeout(function(){

					var reqbody = {};
					reqbody.type = "uploaded";
					WebSocketUtils.send("/appchat/private/ppt/key/"+win.userid, reqbody);
				},15000);
				
			},null,true);
			
//			$("#wjt_user_thumb").attr("src","/attachment/img/"+resp.result.key);
			//"{"status":"200","result":{"path":"http://localhost:8014/attachment/img/ac3f7f1d-4462-4362-8363-8a08826ebe82","key":"ac3f7f1d-4462-4362-8363-8a08826ebe82"}}"
		});

	
	return;
});